@inherits MiniRazor.TemplateBase<CodeEntity>
@namespace OSharp.CodeGeneration.Templates
@using System;
@using System.Collections.Generic;
@using System.Linq;
@using OSharp.CodeGeneration.Services.Entities
@using OSharp.CodeGeneration.Utils
@using OSharp.Collections;
@using OSharp.Exceptions;
@using OSharp.Extensions;
@{
  CodeEntity entity = Model;
  string moduleName = entity.Module.Name;
  string urlModule = entity.Module.Name.UpperToLowerAndSplit();
  string lowerModule = entity.Module.Name.ToLowerCase();
  string keyType = TypeHelper.ToSingleTypeName(entity.PrimaryKeyTypeFullName);
  string entityName = entity.Name;
  string urlEntity = entity.Name.UpperToLowerAndSplit();
  string lowerEntity = entity.Name.ToLowerCase();
  List<CodeProperty> properties = entity.Properties.ToList();
  properties.AddIf(CodeProperty.GetProperty("IsLocked", "是否锁定", typeof(bool), true), entity.HasLocked);
  properties.AddIf(CodeProperty.GetProperty("CreatedTime", "创建时间", typeof(DateTime), false), entity.HasCreatedTime || entity.HasCreationAudited);
  
}// ------------------------------------------------------------------------------
// <once-generated>
//    这个文件只生成一次，再次生成不会被覆盖。
// </once-generated>
//
// <copyright file="src/views/@(lowerModule)/@(lowerEntity)/@(lowerEntity)Store.ts" company="@entity.Module.Project.Company">
//    @entity.Module.Project.Copyright
// </copyright>
// <site>@entity.Module.Project.SiteUrl</site>
// <last-editor>@entity.Module.Project.Creator</last-editor>
// -----------------------------------------------------------------------
import { BasicColumn, BasicTableProps } from '/@@/components/Table';
import { TableStoreBase } from '/@@/store/osharp/TableStoreBase';

export class @(entity.Name)Store extends TableStoreBase {
  protected init() {
    super.init();
    this.boolProps = [@(properties.Where(m => IsPropertyType(m, typeof(bool))).ExpandAndToString(m => $"'{m.Name.ToLowerCase()}'", ", "))];
  }

  protected GetColumns(): BasicColumn[] {
    return [
      { @GetIdColumn(entity) },
@foreach (var prop in properties)
{
<text>      { @GetColumnProps(prop) },
</text>
}
    ];
  }

  protected UpdateTableProps(props: BasicTableProps) {
    props.title = '@(entity.Display)管理';
    return props;
  }
}
@functions
{
  static string GetIdColumn(CodeEntity entity)
  {
    string line = $"title: '编号', dataIndex: 'id', align: 'left', width: {(entity.PrimaryKeyTypeFullName == typeof(int).FullName ? 50 : 300)}";
    return line;
  }

  static string GetColumnProps(CodeProperty prop)
  {
    List<string> list = new List<string>();
    list.Add($"title: '{prop.Display}'");
    list.Add($"dataIndex: '{prop.Name.ToLowerCase()}'");
    list.AddIf("align: 'left'", IsPropertyType(prop, typeof(Guid), typeof(string)));
    list.AddIf($"width: {(IsPropertyType(prop, typeof(bool)) ? 80 : IsPropertyType(prop, typeof(DateTime)) ? 130 : 100)}", !IsPropertyType(prop, typeof(string)));
    list.AddIf($"slots: {{ customRender: '{prop.Name.ToLowerCase()}' }}", IsPropertyType(prop, typeof(bool)));
    list.AddIf($"format: 'date|YYYY/MM/DD HH:mm'", IsPropertyType(prop, typeof(DateTime)));

    return list.ExpandAndToString(", ");
  }

  static bool IsPropertyType(CodeProperty prop, params Type[] types)
  {
    return types.Any(m => m.FullName == prop.TypeName);
  }
}
